/*
 * @Author: yangrongxin
 * @Date: 2021-08-13 09:04:58
 * @LastEditors: guoju
 * @LastEditTime: 2022-01-07 11:28:31
 */
import { useMemo } from 'react';
import { ScrollContent } from '@/components/index';
import ListItem from '../ListItem';
import { TEST_STATUS, PAY_TYPE } from '../../dataDictionary';
import { getSuccessList, getWaitList, getOrderList, getExpireList } from '../../service';
import styles from './index.module.less';

interface IAssessmentListProps {
  type: TEST_STATUS; // 控制当前请求列表的类型
  onDataFetch?: any; // 监听当前数据的变化
}

const AssessmentList = (props: IAssessmentListProps) => {
  const { type, onDataFetch } = props;

  /* 根据当前的测评类型 获取对应的请求方法 */
  const requestFnWithType = useMemo(() => {
    let requestFn: any = null;
    switch (type) {
      // 待测评
      case TEST_STATUS.WAITTEST:
        requestFn = getWaitList;
        break;
      // 待支付
      case TEST_STATUS.WAITPAY:
        requestFn = getOrderList;
        break;
      // 已过期
      case TEST_STATUS.OUTOFDATE:
        requestFn = getExpireList;
        break;
      // 已完成
      case TEST_STATUS.FINISH:
        requestFn = getSuccessList;
        break;
      default:
        break;
    }
    return requestFn;
  }, [type]);

  return (
    <ScrollContent
      onLoadWhenShow
      request={async (data) => requestFnWithType(data)}
      extendScrollContent={styles.scrollContent}
      params={{
        status: type === TEST_STATUS.WAITPAY ? PAY_TYPE.WAITPAY : null,
      }}
      itemProps={{
        status: type === TEST_STATUS.WAITPAY ? PAY_TYPE.WAITPAY : type,
        isOrderList: type === TEST_STATUS.WAITPAY,
      }}
      ScrollItem={ListItem}
      onDataFetch={onDataFetch}
    />
  );
};

export default AssessmentList;
